script_enemy_main{

let playedse=0;
let speed=6;
let shoottime=180;
let size=35;

let shot1=0;
let bullet1=[];
let timer1=[];
let bounce1=[];
let shoot1=[];

let shot2=0;
let bullet2=[];
let timer2=[];

let character="Masahiro";
let cutin=character;
let dispelled=0;
let spellcards=8;
let spellcardnumber=60;
let damagerate=10;
let outfit=1;
outfit=(128*outfit)-128;
let usespell=0;
let bgfade=0;
let frame=0;
let time=0;
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;

let SElaserm1=("\script\SoundEffects\laserm1.wav");
let SEshotm2=("\script\SoundEffects\shotm2.wav");
let SEmagics5=("\script\SoundEffects\magics5.wav");

let BG1=("\script\Images\BackgroundLayers\Nightmare1.png");
let GRboss=("\script\Images\CharacterSprites\"~character~".png");

#include_function "script/Functions/SetSpellcardCommonData.txt";
#include_function "script/Functions/SpellcardNameLoad.txt";
#include_function "script/Functions/HealthBarLoad.txt";
#include_function "script/Functions/CutInLoad.txt";

@Initialize{
	LoadUserShotData("script\shots\ShotsNightmare1.txt");

	LoadSE("\script\SoundEffects\laserm1.wav");
	LoadSE("\script\SoundEffects\shotm2.wav");
	LoadSE("\script\SoundEffects\magics5.wav");

	LoadGraphic("\script\Images\CharacterSprites\"~character~".png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare1.png");

	SetScore(1000000);
	SetLife(500);
	SetTimer(75);
	SetInvincibility(120);
	SetDamageRate(10,10);
	SetEnemyMarker(true);
	MagicCircle(true);
	#include_function "script/Functions/Focus.txt";
	Focus(character);
	SetEffectForZeroLife(60,100,1);
	SetMovePosition02(cx,miny+100,50);
}
	
@MainLoop{

SetCollisionA(GetX,GetY,16);
SetCollisionB(GetX,GetY,16);
SetShotAutoDeleteClip(64,64,64,64);

if(GetCommonData("BombOn")==0){ damagerate=10; if(GetTimer<50){damagerate=20; } }
if(GetCommonData("BombOn")==1){ damagerate=0; }
SetDamageRate(damagerate,damagerate);

let difficulty="";
if(GetCommonDataDefault("Difficulty",2)==1){ difficulty="Easy"; }
if(GetCommonDataDefault("Difficulty",2)==2){ difficulty="Normal"; }
if(GetCommonDataDefault("Difficulty",2)==3){ difficulty="Hard"; }
if(GetCommonDataDefault("Difficulty",2)==4){ difficulty="Lunatic"; }

SpellcardName("Non Rapid Eye Movement [K-Complex]",spellcardnumber); 
HealthBar();
Portrait(cutin,1);

if(time%120==0 && time>=60){
	if(GetPlayerX>minx+50 && GetPlayerX<maxx-50){
	SetMovePosition01(GetPlayerX+rand(-30,30),rand(miny+80,miny+120),1.5);
	}
	if(GetPlayerX<=minx+50){
	SetMovePosition01(GetPlayerX+rand(15,30),rand(miny+80,miny+120),1.5);
	}
	if(GetPlayerX>=maxx-50){
	SetMovePosition01(GetPlayerX-rand(15,30),rand(miny+80,miny+120),1.5);
	}
}


if(time%30==0 && time>=60){
let angle=rand(0,360);
let shot1=0;
	loop(75){
		if(time%60==0){
		CreateShotA(shot1,GetX+12,GetY-30,10);
		SetShotDataA(shot1,0,4,angle,0,-0.1,1.6,44);
		SetShotDataA(shot1,30,NULL,NULL,2,-0.1,1.6,44);
		SetShotDataA(shot1,70,NULL,NULL,0,-0.1,1.6,44);
		FireShot(shot1);
		}
		if(time%60!=0){
		CreateShotA(shot1,GetX+12,GetY-30,10);
		SetShotDataA(shot1,0,4,angle,0,-0.1,1.6,44);
		SetShotDataA(shot1,30,NULL,NULL,-2,-0.1,1.6,44);
		SetShotDataA(shot1,70,NULL,NULL,0,-0.1,1.6,44);
		FireShot(shot1);
		}
	angle+=360/75;
	}
PlaySE(SEshotm2);
usespell=30;
}

if(frame==shoottime){
let angle=GetAngleToPlayer;
	loop(size){
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,GetX,GetY);
	Obj_SetAngle(shot1,angle);
	Obj_SetSpeed(shot1,speed);
	Obj_SetAutoDelete(shot1,false);
	Obj_SetCollisionToPlayer(shot1,false);
	ObjShot_SetBombResist(shot1,true);
	ObjShot_SetGraphic(shot1,2);
	Obj_SetAlpha(shot1,100);
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1];
	timer1[length(bullet1)-1]=0;
	bounce1=bounce1~[shot1];
	bounce1[length(bullet1)-1]=0;
	shoot1=shoot1~[shot1];
	shoot1[length(bullet1)-1]=0;
	angle+=360/size;
	}
if(shoottime>=130){ shoottime-=5; }
if(size<55){ size++; }
PlaySE(SEmagics5);
usespell=-30;
frame=0;
}

let i=0;
while(i<length(bullet1)){
	if(Obj_BeDeleted(bullet1[i])){
	bullet1=erase(bullet1,i); timer1=erase(timer1,i); bounce1=erase(bounce1,i); shoot1=erase(shoot1,i);
	i--;
	}
	else{
	let obj=bullet1[i];
		if(bounce1[i]==0){
		if(Obj_GetX(obj)<=minx){
		Obj_SetAngle(obj,180-Obj_GetAngle(obj));
		Obj_SetX(obj,Obj_GetX(obj)+Obj_GetSpeed(obj));
		shoot1[i]=1;
		}
		if(Obj_GetX(obj)>=maxx){
		Obj_SetAngle(obj,180-Obj_GetAngle(obj));
		Obj_SetX(obj,Obj_GetX(obj)-Obj_GetSpeed(obj));
		shoot1[i]=1;
		}
		if(Obj_GetY(obj)<=miny){
		Obj_SetAngle(obj,-Obj_GetAngle(obj));
		Obj_SetY(obj,Obj_GetY(obj)+Obj_GetSpeed(obj));
		shoot1[i]=1;
		}
		if(Obj_GetY(obj)>=maxy){
		Obj_SetAngle(obj,-Obj_GetAngle(obj));
		Obj_SetY(obj,Obj_GetY(obj)-Obj_GetSpeed(obj));
		shoot1[i]=1;
		}
		}
	Obj_SetSpeed(bullet1[i],Obj_GetSpeed(bullet1[i])-0.025);

if(shoot1[i]==1){
	shot2=(Obj_Create(OBJ_LASER));
	Obj_SetPosition(shot2,Obj_GetX(bullet1[i]),Obj_GetY(bullet1[i]));
	Obj_SetAngle(shot2,Obj_GetAngle(bullet1[i]));
	Obj_SetSpeed(shot2,0);
	ObjLaser_SetLength(shot2,600);
	ObjLaser_SetWidth(shot2,12);
	ObjShot_SetDelay(shot2,45);
	Obj_SetAutoDelete(shot2,false);
	ObjShot_SetBombResist(shot2,true);
	ObjShot_SetGraphic(shot2,50);
	bullet2=bullet2~[shot2];
	timer2=timer2~[shot2];
	timer2[length(bullet2)-1]=0;
if(shoot1[i]==1){ Obj_Delete(bullet1[i]); }
if(playedse==0){ PlaySE(SElaserm1); playedse=8; }
}
	timer1[i]=timer1[i]+1;
	}
i++;
}
if(playedse>0){ playedse--; }

let i=0;
while(i<length(bullet2)){
	if(Obj_BeDeleted(bullet2[i])){
	bullet2=erase(bullet2,i); timer2=erase(timer2,i);
	i--;
	}
	else{
	if(timer2[i]>=120){ ObjShot_FadeDelete(bullet2[i]); }
	timer2[i]=timer2[i]+1;
	}
i++;
}


time++; frame++;
if(usespell>0){ usespell--; } if(usespell<0){ usespell++; }
SetCommonData("Boss1X",GetX); SetCommonData("Boss1Y",GetY);

#include_function "script/Functions/SpellcardName.txt";
#include_function "script/Functions/HealthBar.txt";
#include_function "script/Functions/CutIn.txt";
}

@BackGround{
	if(bgfade<255){ bgfade+=5; }

	SetGraphicScale(1.5,1.5);
	SetRenderState(ALPHA);
	SetAlpha(bgfade);
	SetColor(160+80*cos(time/2),160+80*cos(time/2),160+80*cos(time/2));
	SetTexture(BG1);
	SetGraphicRect(0-time,0,300-time,300);
	SetGraphicAngle(0,0,0);
	DrawGraphic(cx,cy);
}

@DrawLoop{
	SetGraphicScale(1,1);
	SetTexture(GRboss);
	SetGraphicAngle(0,0,0);
	SetColor(120,120,120);
	SetRenderState(ALPHA);

	if(usespell>=1){ SetGraphicRect(384,outfit,512,outfit+128); }
	if(usespell<=-1){ SetGraphicRect(512,outfit,640,outfit+128); }
	if(usespell==0){
		if(GetSpeedX<=0.5 && GetSpeedX>=-0.5){ SetGraphicRect(0,outfit,128,outfit+128); }
		else if(GetSpeedX<-0.5){ SetGraphicRect(128,outfit,256,outfit+128); }
		else if(GetSpeedX>0.5){ SetGraphicRect(256,outfit,384,outfit+128); }
	}
	DrawGraphic(GetX,GetY);
}

@Finalize{
	SetCommonData("NightmareSpellcards",4);
	NewPointData(spellcardnumber,6);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}